<template>
  <div>
      <h1>人员列表</h1>
      <h2 style="color:red">Count组件求和为：{{num}}</h2>
      <input type="text" placeholder="请输入名字" v-model.trim="name" @keydown.enter="addPerson(name)"><button @click="addPerson(name)">添加</button>
      <ul>
          <li v-for="p in persons" :key="p.id">{{p.name}}</li>
      </ul>
  </div>
</template>

<script>
import { mapMutations, mapState } from 'vuex';
import {nanoid} from 'nanoid';
export default {
    data(){
        return {
            name:''
        }
    },
    computed:{
        // 程序员手动获取state
        /* num(){
            return this.$store.state.num;
        } */
        // vuex帮程序员获取state
        ...mapState(['num','persons'])
    },
    methods: {
        // 程序员手动操作Mutations
        addPerson(name){
            const person = {id:nanoid(),name}
            this.$store.commit('ADD_PERSON',person);
            this.name = '';
        },
        // vuex内置方法帮程序员commit
        ...mapMutations({addPerson:'ADD_PERSON'})
    },
}
</script>

<style>

</style>